package xyz.riceball.security.authentication;

import lombok.Getter;
import lombok.Setter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import xyz.riceball.usercore.entity.dto.LoginDTO;

import java.util.Collection;

/**
 * <p>
 * 身份令牌
 * </p>
 *
 * @author xiaovcloud
 * @since 2022/4/25 00:18
 */
@Getter
@Setter
public class RiceBallAuthenticationToken extends AbstractAuthenticationToken {

    private final LoginDTO loginDTO;

    /**
     * 未认证
     * @param loginDTO
     */
    public RiceBallAuthenticationToken(LoginDTO loginDTO) {
        super(null);
        this.loginDTO = loginDTO;
        super.setAuthenticated(false);
    }

    /**
     * 已认证
     * @param authorities
     * @param loginDTO
     */
    public RiceBallAuthenticationToken(Collection<? extends GrantedAuthority> authorities, LoginDTO loginDTO) {
        super(authorities);
        this.loginDTO = loginDTO;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return loginDTO;
    }

    @Override
    public Object getPrincipal() {
        return loginDTO.getUsername();
    }
}
